<template>
  <div class="teamDetail">
    <!-- 上面的部分 -->
    <div class="top">
      <div>
        <!-- 伪类部分 -->
        <div>
          <span class="top-team"> {{ title }} </span>
        </div>
      </div>
    </div>
    <hr style="border: 1px solid #8b8b8b" />

    <div style="margin-top: 20px; margin-left: 5px">团队信息</div>
    <div style="
        margin-top: 20px;
        margin-left: 5px;
        display: flex;
        align-items: center;
      ">
      <div>
        团队编号&nbsp;&nbsp;&nbsp;
        {{ info.number }}
      </div>
      <div style="margin-left: 140px">
        团队名称&nbsp;&nbsp;&nbsp;
        {{ info.tname }}
      </div>
      <div style="margin-left: 100px; display: flex; align-items: center">
        <span> 团队头像</span>
        &nbsp;&nbsp;&nbsp;
        <img style="width: 50px; height: 50px; border-radius: 50%" :src="`http://127.0.0.1:3000/${info.picture}`" />
      </div>
      <div style="margin-left: 100px; display: flex">
        团队标签&nbsp;&nbsp;&nbsp;
        <el-tag v-for="(item, idnex) in info.labelArr" :key="idnex" style="margin-left: 3px">
          {{ item.name }}
        </el-tag>
      </div>
    </div>
    <div style="
        margin-top: 20px;
        margin-left: 5px;
        display: flex;
        align-items: center;
      ">
      <div>
        机构地址&nbsp;&nbsp;&nbsp;
        {{ info.oname }}
      </div>
      <div style="margin-left: 140px">
        团队评分&nbsp;&nbsp;&nbsp;
        <el-rate v-model="info.score" disabled show-score text-color="#ff9900" score-template="{value} "
          class="custom-rate" />
      </div>
      <div style="margin-left: 100px; display: flex; align-items: center">
        <span> 分享量</span>
        &nbsp;&nbsp;&nbsp;
        {{ info.signCount }}
      </div>
      <div style="margin-left: 100px; display: flex">
        签约人数&nbsp;&nbsp;&nbsp;

        {{ info.signCount }}
      </div>
    </div>
    <div style="margin-top: 20px; margin-left: 5px; display: flex">
      <div>邀请加入二维码</div>
      &nbsp;&nbsp;&nbsp;
      <div>
        <img style="width: 140px"
          src="https://qrstyle-api.cli.im/create/createOne?params_data=%255B%257B%2522web_url%2522%253A%2522http%253A%255C%252F%255C%252Fqr61.cn%255C%252FoOm6ZQ%255C%252FqvaZUxD%2522%252C%2522qr_name%2522%253A%2522%2525E5%2525AE%2525B6%2525E5%2525BA%2525AD%2525E5%25258C%2525BB%2525E7%252594%25259F%2525E5%2525B0%25258F%2525E7%2525BB%252584%2522%257D%255D&code_tplid=172872661&code_type=1&time=1693186538&uid=76165228&publickey=a0699df33fe5e5f54ba327aa99765fbb&label_tplid=171" />
        <div style="font-size: 10px">
          医生可通过手机管理端扫描此二维码申请加入团队
        </div>
      </div>
    </div>
    <div style="margin-top: 20px; margin-left: 5px">
      创建时间&nbsp;&nbsp;&nbsp;{{ getCurrentTime() }}
    </div>
    <div style="margin-top: 20px; margin-left: 5px">
      创建人&nbsp;&nbsp;&nbsp;{{ info.dname }}
    </div>
    <div style="margin-top: 20px; margin-left: 5px">
      团队状态&nbsp;&nbsp;&nbsp;
      <el-switch v-model="info.flag" inline-prompt active-text="启用" inactive-text="禁用" />
    </div>
    <hr style="border: 1px solid #8b8b8b; margin-top: 20px" />
    <!-- 医生卡片 -->
    <div style="margin-top: 20px; margin-left: 5px">
      团队成员&nbsp;&nbsp;&nbsp;({{ doctorArr.length }})
    </div>
    <div style="display: flex; align-items: start">
      <div v-for="(item, index) in doctorArr" :key="index" style="width: 24%; margin-top: 20px; margin-left: 10px">
        <el-card shadow="always" style="
            width: 345px;
            height: 180px;
            margin-left: 5px;
            margin-top: 20px;
            position: relative;
          ">
          <div style="display: flex">
            <div style="width: 80px; position: relative">
              <img style="width: 80px; aspect-ratio: 1 / 1" :src="`http://127.0.0.1:3000/${item.picture}`" />
              <span style="
                  font-size: 10px;
                  position: absolute;
                  left: 5px;
                  top: 65px;
                  background-color: orange;
                  padding: 5px;
                  border-radius: 3px;
                  color: white;
                ">{{ item.role }}</span>
            </div>
            <div style="margin-left: 15px">
              <div style="display: flex; align-items: center">
                <div style="margin-right: 10px"></div>
                <div>
                  <span style="font-size: 16px">{{ item.name }}</span>
                  <span style="color: gray; font-size: 10px; margin-left: 10px">{{ item.roleName }}</span>
                </div>
              </div>
              <div style="font-size: 16px; color: gray; margin-top: 10px">
                <span>签约人数</span>&nbsp;&nbsp;&nbsp;<span style="font-size: 16px; color: black">{{ item.signCount }}</span>
              </div>
              <div style="margin-top: 10px">
                <span style="font-size: 16px; color: gray; margin-top: 10px">医生评分</span>
                &nbsp;&nbsp;
                <span style="font-size: 16px; color: gray; margin-top: 10px">
                  {{ item.score }}&nbsp;&nbsp;&nbsp;<span style="font-size: 10px; color: gray">({{ item.scoreCount
                  }}人评价)</span>
                </span>
              </div>
            </div>
          </div>
          <hr style="margin-top: 25px" />
          <div>
            <el-button style="position: absolute; right: 10px; bottom: 10px" @click="doctorInfo(item.id)">医师详情</el-button>
          </div>
        </el-card>
      </div>
    </div>

    <div style="margin-top: 20px">
      <el-button type="primary" style="
          margin-left: 10px;
          margin-right: 10px;
          padding-top: 20px;
          padding-left: 20px;
          padding-right: 20px;
          padding-bottom: 20px;
        " @click="editTeamInfo">编辑团队信息</el-button>
      <el-button type="danger" style="
          margin-right: 20px;
          padding-top: 20px;
          padding-left: 20px;
          padding-right: 20px;
          padding-bottom: 20px;
        " @click="delTeam(info.id)">解散团队</el-button>
      <el-button style="
          margin-right: 20px;
          padding-top: 20px;
          padding-left: 20px;
          padding-right: 20px;
          padding-bottom: 20px;
        " @click="goback">返回</el-button>
    </div>
    <!-- 新增团队的医生详情 -->
    <el-drawer v-model="drawer" title="医生信息" :direction="direction" size="55%">
      <div style="width: 70%">
        <div style="display: flex; justify-content: space-between">
          <div>医生编号：{{ doctorCardInfo.number }}</div>
          <div>医生姓名：{{ doctorCardInfo.name }}</div>
        </div>
        <div style="
            margin-top: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
          ">
          <div style="display: flex; align-items: center">
            <div>医生头像：</div>

            <img style="width: 50px" :src="`http://127.0.0.1:3000/${doctorCardInfo.picture}`" />
          </div>
          <div>性别:{{ doctorCardInfo.gender }}</div>
        </div>
        <div style="margin-top: 20px">角色：{{ doctorCardInfo.role }}</div>
      </div>
      <div style="margin-top: 20px">
        医生简介：{{ doctorCardInfo.description }}
      </div>
      <div style="margin-top: 20px">医生擅长:{{ doctorCardInfo.adept }}</div>
      <hr style="margin-top: 20px" />
      <div style="margin-top: 20px; display: flex">
        <div>加入团队&nbsp;&nbsp;&nbsp;</div>
        <div>
          <div style="margin-bottom: 20px" v-for="(item, index) in teamDetail" :key="index">
            {{ item.oname }}/{{ item.tname }}
          </div>
        </div>
      </div>
      <div>
        签约人数&nbsp;&nbsp;&nbsp;{{ doctorCardInfo.signCount.signCount }}
      </div>
      <div style="margin-top: 20px">
        创建时间&nbsp;&nbsp;&nbsp;{{ doctorCardInfo.createTime }}
      </div>
      <div style="margin-top: 20px">
        创建人&nbsp;&nbsp;&nbsp;{{ doctorCardInfo.creatorName }}
      </div>
      <div style="margin-top: 20px">
        账号状态:
        <el-switch v-model="value3" inline-prompt active-text="启用" inactive-text="禁用" />
      </div>
      <div style="margin-top: 20px">
        <el-button type="primary" @click="goto(doctorCardInfo.id)">编辑医生信息</el-button>
      </div>
    </el-drawer>
    <!-- 解散团队的dialog -->
    <el-dialog v-model="innerVisible" width="30%" title="解散团队" append-to-body style="position: relative">
      <div style="margin-bottom: 60px">团队解除后无法恢复,确认解除吗？</div>
      <div style="position: absolute; right: 10px; bottom: 10px">
        <el-button type="primary" @click="realDel">确认</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
    <!-- 编辑团队的dialog -->
    <el-dialog v-model="dialogFormVisible" title="编辑团队信息" style="width: 500px">
      <el-form :model="form">
        <el-form-item label="团队名称">
          <el-input autocomplete="off" style="width: 240px" v-model="form.tname" />
        </el-form-item>
        <el-form-item label="所属机构">
          <el-select v-model="form.oname" @change="getOragn">
            <el-option v-for="(item, index) in organTeam" :key="index" :label="(item.name as string)" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="团队头像">
          <UploadTeaM @getPicUrl="getPic" :picture="pic" :flag="true"></UploadTeaM>
        </el-form-item>
        <el-form-item label="团队标签">
          <el-select placeholder="请选择" style="width: 240px" v-model="arr7" multiple>
            <el-checkbox v-model="allselect"> </el-checkbox>全选
            <el-option v-for="(item, index) in arr8" :key="index" :label="item.name" :value="item.id">
              <template #default>
                <el-checkbox v-model="arr9[index]" @change="fn1"></el-checkbox>
                {{ item.name }}
              </template>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="editFormInfo"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { useRoute, useRouter } from 'vue-router'
import { ref } from 'vue'
import api from '@/api'
import { ElMessage } from 'element-plus'

//导入upload

import UploadTeaM from '@/components/zxy/UploadTeam.vue'

// 导入状态机

import teamStore from '@/store/zxy/team'
import { storeToRefs } from 'pinia'

const arr9 = ref([])
const fn1 = () => {
  allselect.value = arr9.value.every((item) => item)
}
//抽屉
const direction = ref('rtl')
const drawer = ref(false)

// ---------------------------
const route = useRoute()
const router = useRouter()
const title = ref('')
const allselect = ref()
// 根据团队id获取所有的label标签
const labelObj = ref({
  teamId: '',
})

// 导状态机的
const store = teamStore()
const { infoEdit } = storeToRefs(store)

// 编辑的表单
const pic = ref({ picure: '' })
const form = ref({
  picture: '',
  tname: '',
  oname: '',
})

// 编辑表单的参数
const editFormObj = ref({
  picture: '',
  name: '',
  id: '',
  organId: '',
  labelIdStr: '',
})

// console.log('状态机取到的值', infoEdit)
if (infoEdit.value) {
  form.value.tname = infoEdit.value.tname
  pic.value.picure = infoEdit.value.picture
  // console.log('图片', pic.value.picure)
}

// 将传递过来的值赋给变量其中status判断是管理的还是加入的
const info = ref({
  status: 0,
  number: 0,
  tname: '',
  picture: '',
  labelArr: [{ name: '' }],
  oname: '',
  score: '',
  signCount: '',
  flag: false,
  dname: '',
  id: '',
  name: '',
  owerId: '',
})

if (route.query.info) {
  info.value = JSON.parse(route.query.info as string)
  if (info.value && info.value.status === 0) {
    title.value = '我管理的团队详情'
  } else {
    title.value = '我加入的团队详情'
  }
}

//将传递过来的teamid赋给参数
editFormObj.value.id = info.value.id
// 将id传给获取label的参数
labelObj.value.teamId = info.value.id

// 创建当前时间
const getCurrentTime = () => {
  const currentTime = new Date()
  const formattedTime = currentTime.toLocaleString() // 格式化为本地时间字符串
  return formattedTime
}

const DoctorName = ref({
  teamId: '',
})

if (info.value.id) {
  DoctorName.value.teamId = info.value.id
}

// 根据团队id获取医生数据

const doctorArr = ref([
  {
    name: '',
    id: '',
    picture: '',
    role: '',
    adept: '',
    createTime: '',
    creatorId: '',
    description: '',
    flag: '',

    msStatus: '',
    number: '',
    roleName: '',
    score: '',
    scoreCount: '',
    signCount: '',
    status: '',
    tel: '',
    tstatus: '',
    userId: '',
  },
])
const getDoctorInfo = async () => {
  // console.log('传递的参数', DoctorName.value)
  const res = await api.team.getDoctorInfoByTeamId(DoctorName.value)
  // console.log('获取的结果详细信息', res)
  doctorArr.value = res.data.data
}
const arr2 = ref([
  {
    name: '',
  },
])
// 将获取的数据结果id查找医生的名字
const getDoctorName = async () => {
  const res = await api.team.getDoctorBasicInfo(DoctorName.value)
  arr2.value = res.data.data
  // console.log('基本信息', res.data.data)
  // 合并
  twoArr()
}

// 将两个数组的信息合并
const twoArr = () => {
  for (let i = 0; i < doctorArr.value.length; i++) {
    doctorArr.value[i].name = arr2.value[i].name
  }
  // console.log('最终心态的数组', doctorArr.value)
}

// 通过医生id查找抽屉里用户的信息
const doctorObj = ref({
  id: '',
})

// 详情页面的团队
const teamDetail = ref([
  {
    oname: '',
    tname: '',
  },
])

// 医生状态的开关
const value3 = ref(0)
// 查赵抽屉
const doctorInfo = (s: string) => {
  drawer.value = true
  doctorObj.value.id = s
  console.log('抽屉参数', doctorObj.value)
  getDoctorDetail()
}

const doctorCardInfo = ref({
  name: '',
  role: '',
  picture: '',
  number: '',
  description: '',
  adept: '',
  signCount: {
    signCount: '',
  },
  createTime: '',
  creatorName: '',
  flag: 0,

  gender: '',
  id: '',
})

const getDoctorDetail = async () => {
  const res = await api.team.getDoctorDetailApi(doctorObj.value)
  console.log('获取结果', res.data.data)
  teamDetail.value = res.data.data.team
  doctorCardInfo.value = res.data.data
}

// 控制弹窗打开关闭的
const innerVisible = ref(false)
const delObj = ref({
  id: '',
})

// 解散团队
const delTeam = (s: string) => {
  // 打开弹窗
  innerVisible.value = true
  console.log(s, '删除的id')
  // 删除的参数赋值
  delObj.value.id = s
}

// 弹窗的取消按钮
const cancel = () => {
  // 关闭弹窗
  innerVisible.value = false
}
// 弹窗的确定按钮
const realDel = async () => {
  // 关闭弹窗
  innerVisible.value = false
  // 调用接口进行删除
  const res = await api.team.delTeamByTeamidApi(delObj.value)
  if (res.data.code === 200) {
    ElMessage({
      message: res.data.msg,
      grouping: true,
      type: 'success',
    })
    router.push('td')
  } else {
    ElMessage({
      message: res.data.msg,
      grouping: true,
      type: 'error',
    })
  }
  // 返回上个页面
}

// 返回按钮
const goback = () => {
  router.push('td')
}

// 团队编辑
// 团队编辑的dialog

const dialogFormVisible = ref(false)

const editTeamInfo = () => {
  dialogFormVisible.value = true
  form.value.oname = info.value.owerId
}

const getPic = (arg: string) => {
  console.log(arg)
  // 将请求参数进行重新赋值
  form.value.picture = ''
  form.value.picture = arg
  // 编辑的参数
  editFormObj.value.picture = arg
}

// 跳转到医生编辑页面
const goto = (id: string) => {
  // console.log('id', id)
  router.push(`/home/institution/addyx?${id}`)
}

const organTeam = ref([
  {
    name: '',
    id: '',
  },
])

// 查询所有的机构表
const getAllOrgan = async () => {
  const res = await api.team.getAllChcsOrgan()
  console.log('所有的机构表数据', res.data.data.rows)
  organTeam.value = res.data.data.rows
}

// 当下拉框发生变化时候
const getOragn = (s: string) => {
  editFormObj.value.organId = s
  // console.log(s)
}

// 点击团队编辑按钮确定
const editFormInfo = async () => {
  editFormObj.value.name = info.value.tname
  editFormObj.value.labelIdStr = JSON.stringify(arr7.value)
  editFormObj.value.organId = form.value.oname
  if (!form.value.picture) {
    editFormObj.value.picture = infoEdit.value.picture
  }
  console.log('编辑的参数', editFormObj.value)
  // 抓json字符串

  // editFormObj.value.labelIdStr = JSON.stringify(editFormObj.value.labelIdStr)
  // editFormObj.value.labelIdStr = JSON.stringify(editFormObj.value.labelIdStr)

  console.log('参数', editFormObj.value)
  const res = await api.team.modifyTeamInfoApi(editFormObj.value)
  dialogFormVisible.value = false
  if (res.data.code === 200) {
    ElMessage({
      message: res.data.msg,
      grouping: true,
      type: 'success',
    })
    // router.push('team')
  } else {
    ElMessage({
      message: res.data.msg,
      grouping: true,
      type: 'error',
    })
  }
}

const arr7 = ref([])

const arr8 = ref([
  {
    id: '',
    name: '',
  },
])

// 查找本团队所有的label标签
const getLabel = async () => {
  const res = await api.team.getAlllabelByTeamId(labelObj.value)

  console.log('本机构的标签查找结果', res.data.data.labelArr)
  arr7.value = res.data.data.labelArr.map((item: any) => item.id)

  //  res.data.data.labelArr
}

let getLabelData = ref({
  searchKey: '',
  page: 1,
  count: 10,
})
// 查找所有的label标签
const getAllLabel = async () => {
  const res = await api.team.getTeamLabel(getLabelData.value)
  console.log('所有标签', res.data.data.result)
  arr9.value = res.data.data.result.map(() => false)
  console.log('-------------------', arr9.value)
  arr8.value = res.data.data.result
}
if (labelObj.value) {
  getLabel()
}
getAllLabel()
getDoctorInfo()
getDoctorName()
getAllOrgan()
</script>

<style lang="scss" scoped>
.teamDetail {
  width: 100vw;

  background-color: #fff;
}

.top-team::before {
  margin-left: 5px;
  margin-top: 5px;
  content: '';
  display: inline-block;
  width: 10px;
  height: 30px;
  background-color: skyblue;
  margin-right: 10px;
  border-radius: 6px;
  vertical-align: middle;
  /* 垂直居中对齐 */
}

.top-team {
  margin-top: 20px;
  font-size: 20px;
  display: flex;
  align-items: center;
}

.menu-wrapper {
  display: flex;
  flex-grow: 1;
}

.el-menu-demo .el-menu-item {
  text-align: left;
}

.top {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
</style>
